$(document).ready(function() {
    notifications = new Array();
    // интервал времени задаётся в милисекундах,
    // т.е. значение 3000 соответствует 3 секундам
    window.setTimeout("checkNotifications();", 2000);

    $('#header_auth_info').dropDownMenu();


    $("#button_id").click(function() {
        alert("Hello");
        $.get("getUsers.do", function(data){
            alert("date:" + data);
        });
    });

    $("#auth_form").submit(function() {
        var user = $("#auth_form").serializeObject();
        $.ajax({
            type: "post",
            contentType: "application/json",
            url: "auth.do",
            data: JSON.stringify(user),
            success: function(data) {
                $("#auth_info").text(data);
                $("#login_table").addClass("invisible");
            },
            error: function(data) {
                alert("error data = " + data);
            }
        });
        return false;
    });



    $("#logout").click(function() {
            $.post("unauth.do", function(data){
                $("#auth_info").text(data);
                $("#login_table").removeClass("invisible");
            });
        }
    );

    $("#send_message_button").click(function(){
        $.post("sendMessage.do", {"message": $("#message_input").val()}, function(data){
            alert("data гг= " + data);
        });
    });

    //send message when click button send or ctrl + enter
    $('#send_message').submit(sendMessage);
    $('#message').keydown(function(e){
        if (e.ctrlKey && e.which == 13){
            e.preventDefault();
            sendMessage();
        }

    });

    $('#chat_minimize_button').on('click', function(){
        $('#message_box_container').slideToggle(200);
    });

});

function checkNotifications()
{
    $.ajax({
        type: "post",
        dataType: "json",
        url: "checkNotifications.do",
        success: function(data) {

            jQuery.each(data, function(i, notification){
                if (!$(notifications).isContains(notification)){
                    addNotification(notification)
                }
            });

        },
        error: function(data) {
            alert("error data = " + data);
        }
    });

    window.setTimeout("checkNotifications();", 2000);
}

function sendMessage(){
    if ($('#message').val == null || $('#message').val() == ''){
        return false;
    }
    $.post('sendMessage.do', {'message': $('#message').val()}, function(message){
        addNotification(message);
        $('#message').val('');
    });
    return false;
}

function getFormatDate(milisec){
    var dateTime = new Date(milisec);
    var str = $.format.date(dateTime, 'dd.MM.yyyy HH:mm:ss');
    return str;
}

function addNotification(notification){
    notifications.push(notification);
    if (notification.notificationType === "MESSAGE"){
        var owner = notification.owner === null ? "Гость" : notification.owner.username;
        $('#message_box').append(getFormatDate(notification.createDate) + ' ' + owner + ': ')
        $('#message_box').append(notification.text + "\n");
    }
}